package hector.tang.controller;

import hector.tang.bean.Account;
import hector.tang.bean.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 02
 * 请求参数绑定
 */
@Controller
@RequestMapping("/param")
public class ParamController {

    /**
     * 请求参数绑定
     *  <a href="param/testParam?username=tom">请求参数绑定</a>
     *  springmvc会将请求路径携带的参数自动的赋值到方法的参数
     */
    @RequestMapping("/testParam")
    public String testParam(String username){
        System.out.println("执行了testParam  username : "+username);
        return "success";
    }

    /**
     * 请求参数绑定到一个javaBean中 请求的表单属性名称和bean对象属性对应就行
     *     <form action="param/testBean" method="post">
     *         姓名:<input type="text" name="username" /> <br/>
     *         密码:<input type="text" name="password" /> <br/>
     *         money:<input type="text" name="money" /> <br/>
     *         <input type="submit" value="提交">
     *     </form>
     * @param account
     * @return
     */
    @RequestMapping("/testBean")
    public String testBean(Account account){
        System.out.println("执行了testBean");
        System.out.println(account.toString());
        return "success";
    }

    /**
     * 请求参数绑定到javaBean对象中，此bean对象还包含其他引用对象属性
     *          <form action="param/testBean2" method="post">
     *         姓名:<input type="text" name="username" /> <br/>
     *         密码:<input type="text" name="password" /> <br/>
     *         money:<input type="text" name="money" /> <br/>
     * <%--        将用户名和年龄封装到Account对象中的属性user中 --%>
     *         用户名:<input type="text" name="user.uname" /> <br/>
     *         用户年龄:<input type="text" name="user.age" /> <br/>
     *         <input type="submit" value="提交">
     *     </form>
     * @param account
     * @return
     */
    @RequestMapping("/testBean2")
    public String testBean2(Account account){
        System.out.println("执行了testBean2");
        System.out.println(account.toString());
        return "success";
    }


    @RequestMapping("testBean3")
    public String testBean3(Account account){
        System.out.println("执行了testBean3");
        System.out.println(account.toString());
        account.getMap().get("one");
        return "success";
    }

    @RequestMapping("/saveUser")
    public String saveUser(User user){
        System.out.println(user.toString());
        return "success";
    }

}
